package org.oneblog.controller;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionController extends BaseController {

    public static final String SESSION_ID = "wjyuian_session_id";

    public static String randon() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static void setLoginSession(HttpServletRequest request, HttpServletResponse response, String account) {

        HttpSession session = request.getSession();

        String token = randon();

        session.setAttribute(SESSION_ID, token);
    }

    public static String getSessionToken(HttpServletRequest request, HttpServletResponse response) {
        Object s = request.getSession().getAttribute(SESSION_ID);

        return s == null ? "" : s.toString();
    }

    public static String getBasePath(HttpServletRequest request) {
    	String protocal = request.getScheme();
    	
    	String serverName = request.getServerName();
    	
    	int port = request.getServerPort();
    	
    	String portString = "";
    	
    	String context = request.getContextPath();
    	
    	if(serverName.equalsIgnoreCase("localhost")) {//本地
    		portString = ":" + port;
    	} else {
    		protocal = "https";
    	}
    	
        String basePath = protocal + "://" 
                + serverName + portString + 
                context + "/";
        
        return basePath;
    }
}
